var _util = _interopRequireDefault(require("./util")), _diff = _interopRequireDefault(require("./diff")), _eventEmitter = _interopRequireDefault(require("./event-emitter"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _toConsumableArray(t) {
    return _arrayWithoutHoles(t) || _iterableToArray(t) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(t) {
    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t)) return Array.from(t);
}

function _arrayWithoutHoles(t) {
    if (Array.isArray(t)) {
        for (var e = 0, r = new Array(t.length); e < t.length; e++) r[e] = t[e];
        return r;
    }
}

function _slicedToArray(t, e) {
    return _arrayWithHoles(t) || _iterableToArrayLimit(t, e) || _nonIterableRest();
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}

function _iterableToArrayLimit(t, e) {
    var r = [], n = !0, i = !1, a = void 0;
    try {
        for (var o, s = t[Symbol.iterator](); !(n = (o = s.next()).done) && (r.push(o.value), 
        !e || r.length !== e); n = !0) ;
    } catch (t) {
        i = !0, a = t;
    } finally {
        try {
            n || null == s.return || s.return();
        } finally {
            if (i) throw a;
        }
    }
    return r;
}

function _arrayWithHoles(t) {
    if (Array.isArray(t)) return t;
}

function _typeof(t) {
    return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
        return typeof t;
    } : function(t) {
        return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t;
    })(t);
}

var isFunction = _util.default.isFunction;

try {
    Promise = require("./promise.js");
} catch (t) {
    wx.showModal({
        title: "Promise错误",
        content: "需要更新Promise引入机制"
    }), console.error(t);
}

var originPage = Page, pages = {}, channel = {}, routers = {
    go: wx.navigateTo,
    back: wx.navigateBack,
    tab: wx.switchTab,
    redirect: wx.redirectTo,
    relaunch: wx.reLaunch
}, route = function(o, t) {
    var s = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : "go";
    return "string" == typeof t ? s = t || s : "object" === _typeof(t) && (o += "?".concat(_util.default.serialize(t))), 
    new Promise(function(t, e) {
        var r = {
            success: t,
            fail: e
        };
        if (/^back/.test(o)) {
            var n = _slicedToArray(o.match(/\d/) || [], 1)[0], i = void 0 === n ? 1 : n;
            s = "back", r.delta = +i;
        } else routers[s] || (s = "redirect"), r.url = o;
        try {
            var a = _util.default.parseUrl(r.url);
            pages[a.url].onNavigate && pages[a.url].onNavigate(a.query);
        } catch (t) {}
        routers[s](r);
    });
}, setData = function(t, e) {
    var r = this, n = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : function() {}, i = {};
    if ("string" == typeof t) {
        var a = {};
        for (var o in e) e.hasOwnProperty(o) && (a[t + "." + o] = e[o]);
        i = a;
    } else _util.default.isObject(t) && (n = e || n, i = t);
    var s = (0, _diff.default)(i, this.$$data || this.data);
    this.$$data && (this.$$data = null), Object.keys(s).length ? Promise.resolve().then(function() {
        return r.setData(s, n);
    }) : n();
}, destory = function() {
    this.$removeAllListeners();
    var e = this.$$observer;
    e && Object.keys(e).forEach(function(t) {
        return e[t].disconnect();
    }), this.$$observer = null;
}, addListener = function(n, t) {
    var i = this, a = !(2 < arguments.length && void 0 !== arguments[2]) || arguments[2];
    this.$$events = this.$$events || [];
    var o = isFunction(t) ? t : function() {};
    return t = function() {
        for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
        if (i.__hide && a) return i.$$subscribe = i.$$subscribe || {}, i.$$subscribe[n] = {
            handler: o,
            args: e
        };
        o.apply(void 0, e);
    }, this.$$events.push([ n, t ]), _eventEmitter.default.addListener(n, t);
}, listenerEmit = function(i) {
    var a = this;
    this.__hide = !1;
    var o = this.$$subscribe || {};
    Object.keys(o).forEach(function(t) {
        var e = o[t], r = e.handler, n = e.args;
        console.log("".concat(i, ": ").concat(a.route || a.is, ", eventName: ").concat(t)), 
        r.call.apply(r, [ a ].concat(_toConsumableArray(n))), delete o[t];
    });
};

function _Page() {
    var r = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {}, t = (pages[__wxRoute] = r).mixins;
    if (Array.isArray(t)) {
        delete r.mixins;
        var a = [ "onLoad", "onUnload", "onShow", "onHide", "onPullDownRefresh", "onReachBottom" ];
        t.forEach(function(e) {
            Object.keys(e).forEach(function(t) {
                if ("data" === t) r.data = Object.assign({}, e.data, r.data || {}); else if (isFunction(e[t]) && a.includes(t)) {
                    var n = e[t], i = r[t];
                    r[t] = function() {
                        for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
                        i && i.call.apply(i, [ this ].concat(e)), n.call.apply(n, [ this ].concat(e));
                    };
                } else r[t] || (r[t] = e[t]);
            });
        });
    }
    r.$route = route, r.$put = function(t, e) {
        channel[t] = e;
    }, r.$take = function(t) {
        var e = channel[t];
        return channel[t] = null, e;
    }, r.$digest = function() {
        this.$$data = JSON.parse(JSON.stringify(this.data));
    }, r.$setData = function() {
        for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
        setData.apply(this, e);
    }, r.$addListener = function() {
        for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
        return addListener.apply(this, e);
    }, r.$addListeners = function(t) {
        var e = this;
        t.forEach(function(t) {
            return e.$addListener.apply(e, _toConsumableArray(t));
        });
    }, r.$removeAllListeners = function() {
        this.$$events && this.$$events.forEach(function(t) {
            return _eventEmitter.default.removeListener.apply(_eventEmitter.default, _toConsumableArray(t));
        });
    }, r.$emit = _eventEmitter.default.emit;
    var n = r.onUnload, l = r.onLoad, i = r.onShow, o = r.onHide;
    return r.onLoad = function(t) {
        this.$app = getApp(), this.$appData = this.$app.globalData, this.$report = function(t, e) {
            this.$app.reportAnalytics(t, e);
        }, this.__query = t;
        try {
            if (this.route.startsWith("miniweb")) {
                var e = getApp().globalData, r = t.fromuid || e.fromuid || "", n = t.cardid || e.fromcardid || "", i = t.guid || e.fromguid || "", a = t.sid || e.fromsid || "";
                e.fromuid = r, e.fromcardid = n, e.fromguid = i, e.fromsid = a, this.__miniwebquery = {
                    fromuid: r,
                    source: e.fromapp,
                    cardid: n,
                    guid: i,
                    sid: a
                }, this.$goHome = function(t, e) {
                    var r = this.__miniwebquery, n = r.guid, i = r.cardid, a = getCurrentPages(), o = a.length;
                    if (1 === o) this.$route("/pages/card/view/view?guid=".concat(n, "&cardid=").concat(i), "redirect"); else {
                        var s = a[o - 2].route, u = [ "miniweb/pages/website/index/index" ];
                        "website" !== e && u.push("pages/tabbar/card/card", "pages/card/view/view"), u.includes(s) ? this.$route("back") : isFunction(t) && t();
                    }
                };
            }
        } catch (t) {
            console.log("miniweb-page-enhance", t);
        }
        for (var o = arguments.length, s = new Array(1 < o ? o - 1 : 0), u = 1; u < o; u++) s[u - 1] = arguments[u];
        l && l.call.apply(l, [ this, t ].concat(s));
    }, r.onUnload = function() {
        for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
        isFunction(n) && n.apply(this, e), destory.call(this);
    }, r.onShow = function() {
        listenerEmit.call(this, "page");
        for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
        isFunction(i) && i.apply(this, e);
    }, r.onHide = function() {
        this.__hide = !0;
        for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
        isFunction(o) && o.apply(this, e);
    }, originPage(r);
}

Page = function(t) {
    return _Page(t);
}, Page.originPage = originPage;

var originComponent = Component;

function _Component() {
    var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {}, e = t.created, r = t.detached;
    t.created = function() {
        this.$app = getApp(), this.$appData = this.$app.globalData, this.$report = this.$app.reportAnalytics, 
        isFunction(e) && e.call(this);
    }, t.detached = function() {
        isFunction(r) && r.call(this), destory.call(this);
    };
    var n = t.pageLifetimes || {}, i = n.show, a = n.hide, o = n.resize;
    return t.pageLifetimes = {
        show: function() {
            listenerEmit.call(this, "component");
            for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
            isFunction(i) && i.apply(this, e);
        },
        hide: function() {
            this.__hide = !0;
            for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
            isFunction(a) && a.apply(this, e);
        },
        resize: function() {
            for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
            isFunction(o) && o.apply(this, e);
        }
    }, t.methods = t.methods || {}, Object.assign(t.methods, {
        $route: route,
        $put: function(t, e) {
            channel[t] = e;
        },
        $take: function(t) {
            var e = channel[t];
            return channel[t] = null, e;
        },
        $digest: function() {
            this.$$data = _util.default.deepClone(this.data);
        },
        $setData: function() {
            for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
            setData.apply(this, e);
        },
        $addListener: function() {
            for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
            return addListener.apply(this, e);
        },
        $addListeners: function(t) {
            var e = this;
            t.forEach(function(t) {
                return e.$addListener.apply(e, _toConsumableArray(t));
            });
        },
        $removeAllListeners: function() {
            this.$$events && this.$$events.forEach(function(t) {
                return _eventEmitter.default.removeListener.apply(_eventEmitter.default, _toConsumableArray(t));
            });
        },
        $emit: _eventEmitter.default.emit
    }), originComponent(t);
}

Component = function(t) {
    return _Component(t);
}, Component._ = originComponent;